﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Buy2Site.Classes;

namespace Buy2Site.UserControls.ShoppingCart
{
    public partial class ShoppingCartItem : System.Web.UI.UserControl
    {
        //public List<ProductClass> ListOfItems { get; set; }
        //public List<ProductClass> ListOfAllQuantetiesItems { get; set; }

        //public string ProductName { get; set; }
        //public int ProductQuatity { get; set; }
        public static string Sold { get; set; }

        public static ProductClass ProductItem { get; set; }
        public static int CountOfItems { get; set; }
        public static double PriceOfAllSameItems { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["ListOfItems"] != null)
            {
                //if (ProductItem != null)
                //{
                // chkbItem.Text = ProductItem.Title;
                List<ProductClass> list = ((ProductClass)Session["ListOfItems"]).ListOfItems;
                // query all items that have the same title
                CountOfItems = list.Select(element => element.Title).Count();
                PriceOfAllSameItems = list.Select(element => element.Price).Sum();
                string productID = Request.QueryString["productID"];

                // get item quantity
                //foreach (var item in list)
                //{
                //    //if(item.Title==ProductItem.Title)
                //    //{

                //    var price = list
                //    //PriceOfAllSameItems += el.Price;
                //    foreach (var el in items)
                //    {
                //        countOfItems++;
                //        PriceOfAllSameItems += el.Price;
                //    }

                //    //}
                //}

                // set items number
                //lblItemQuantity.Text = Convert.ToString(CountOfItems);

                // set item price
                //lblItemPrice.Text = Convert.ToString(PriceOfAllSameItems);
                UpdateItemQuantityAndPrice();
                // set itme title with link to more details page
                // TODO: add variables for the product details
                // TODO: each link should be with variables to the product details

                chkbItem.Text = "<a href='~/Pages/ProductDetailsPage.aspx" + "?productID=" + productID + "'>" +
                                "Product Details</a>";
                //}
            }
        }

        protected void chkbItem_CheckedChanged(object sender, EventArgs e)
        {            
            // remove item to shopping cart
            if (!chkbItem.Checked)
            {
                ((ProductClass)Session["ListOfItems"]).ListOfItems.Remove(ProductItem);
                ((ProductClass)Session["ListOfAllQuantetiesItems"]).ListOfAllQuantetiesItems.Add(ProductItem);
                CountOfItems--;
                PriceOfAllSameItems = PriceOfAllSameItems - ProductItem.Price;
                UpdateItemQuantityAndPrice();

                //CartItemsManager.RemoveFromCList(new ProductClass() { Title = chkbItem.Text });
                //Application["ShoppingCart"] = CartItemsManager.ListOfProducts;
                
                //ListOfItems.Add(
                //Session["ListOfItems"]
            }
            // add item to shopping cart
            else
            {
                ((ProductClass)Session["ListOfItems"]).ListOfItems.Add(ProductItem);
                ((ProductClass)Session["ListOfAllQuantetiesItems"]).ListOfAllQuantetiesItems.Remove(ProductItem);
                CountOfItems++;
                PriceOfAllSameItems = PriceOfAllSameItems + ProductItem.Price;
                UpdateItemQuantityAndPrice();
            }
        }
        private void UpdateItemQuantityAndPrice()
        {
            // set items number
            lblItemQuantity.Text = Convert.ToString(CountOfItems);

            // check if member is logged in
            if (Session["User"] != null)
            {
                // set item price - get 10% discount
                lblItemPrice.Text = Convert.ToString(PriceOfAllSameItems * 0.9);
            }
            // not member (guest) - pays full price
            else
            {
                // set item price 
                lblItemPrice.Text = Convert.ToString(PriceOfAllSameItems);
            }
            
        }
    }
}